package com.tjsinfo.tjsoa.articles.bo;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHQLDao;
import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.articles.vo.TRules;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.system.vo.User;

public class RulesBO implements IRulesBO {
	
	private IQbcDao qbcDao;
	private IHibernateDao hibernateDao;
	private IJDBCDao jdbcDao;
	private IHQLDao hqlDao;
	
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}


	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}


	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}


	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}


	public IJDBCDao getJdbcDao() {
		return jdbcDao;
	}


	public void setJdbcDao(IJDBCDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}


	public IHQLDao getHqlDao() {
		return hqlDao;
	}


	public void setHqlDao(IHQLDao hqlDao) {
		this.hqlDao = hqlDao;
	}

	
	/**
	 * 根据cid及检索条件ssearch查找文章列表
	 * @param cid
	 * @return 返回符合条件的分页显示文章列表
	 */
	public PaginationSupport findManageBySearch(String sstart,String slimit,User user,HttpServletRequest request){
		// TODO Auto-generated method stub
		
				DetachedCriteria criteria = DetachedCriteria.forClass(TRules.class);
				String title=request.getParameter("title");  //发文时间
		 		if (!"".equals(title) &&  title!=null){		 		
		 			criteria.add(
		 					Restrictions.like("title", title,MatchMode.ANYWHERE)
		 					);
		 		}
		 		String wenhao=request.getParameter("wenhao");  //发文时间
		 		if (!"".equals(wenhao) &&  wenhao!=null){		 		
		 			criteria.add(
		 					Restrictions.like("wenhao", wenhao,MatchMode.ANYWHERE)
		 					);
		 		}
		 		String danwei=request.getParameter("danwei");  //发文时间
		 		if (!"".equals(danwei) &&  danwei!=null){		 		
		 			criteria.add(
		 					Restrictions.like("danwei", danwei,MatchMode.ANYWHERE)
		 					);
		 		}
		 		String bumen=request.getParameter("bumen");  //发文时间
		 		if (!"".equals(bumen) &&  bumen!=null){		 		
		 			criteria.add(
		 					Restrictions.like("bumen", bumen,MatchMode.ANYWHERE)
		 					);
		 		}
		 		String tablename=request.getParameter("tablename");  //发文时间
		 		if (!"".equals(tablename) &&  tablename!=null){		 		
		 			criteria.add(
		 					Restrictions.like("tablename", tablename,MatchMode.ANYWHERE)
		 					);
		 		}
				String createdatestr=request.getParameter("createdatestr");  //发文时间
		 		if (!"".equals(createdatestr) &&  createdatestr!=null){
		 			String[] createdatestrArray = createdatestr.split("/");
		 			createdatestr = createdatestrArray[2]+"-"+createdatestrArray[0]+"-"+createdatestrArray[1];
		 			criteria.add(
		 					Restrictions.sqlRestriction("convert(char(10),createdate,120) = ?", createdatestr, Hibernate.STRING)
		 					);
		 		}
		 		String uploaddatestr=request.getParameter("uploaddatestr");  //上传时间
		 		if (!"".equals(uploaddatestr) &&  uploaddatestr!=null){
		 			String[] uploaddatestrArray = uploaddatestr.split("/");
		 			uploaddatestr = uploaddatestrArray[2]+"-"+uploaddatestrArray[0]+"-"+uploaddatestrArray[1];
		 			criteria.add(
		 					Restrictions.sqlRestriction("convert(char(10),uploaddate,120) = ?", uploaddatestr, Hibernate.STRING)
		 					);
		 		}
		 		
				String department = user.getDepartment();
				String[] departmentArray = department.split(",");
				List<Integer> list  = new ArrayList<Integer>();
				for (String id : departmentArray) {
					if(!"".equals(id) && id != null ){
						list.add(Integer.parseInt(id));
					}
				}
				if(!"系统管理员".equals(user.getUsername())){
					criteria.add(Restrictions.in("bumenid", list));
				}
				List<Order> orderlist = new ArrayList<Order>();
				orderlist.add(Order.desc("bumenid"));
				orderlist.add(Order.desc("state"));
				orderlist.add(Order.desc("createdate"));
				PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
				return ps;
	}
	
	
	public PaginationSupport findBySearch(String sstart, String slimit,HttpServletRequest request) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TRules.class); 
		String title=request.getParameter("title");  //发文时间
 		if (!"".equals(title) &&  title!=null){		 		
 			criteria.add(
 					Restrictions.like("title", title,MatchMode.ANYWHERE)
 					);
 		}
 		String wenhao=request.getParameter("wenhao");  //发文时间
 		if (!"".equals(wenhao) &&  wenhao!=null){		 		
 			criteria.add(
 					Restrictions.like("wenhao", wenhao,MatchMode.ANYWHERE)
 					);
 		}
 		String danwei=request.getParameter("danwei");  //发文时间
 		if (!"".equals(danwei) &&  danwei!=null){		 		
 			criteria.add(
 					Restrictions.like("danwei", danwei,MatchMode.ANYWHERE)
 					);
 		}
 		String bumen=request.getParameter("bumen");  //发文时间
 		if (!"".equals(bumen) &&  bumen!=null){		 		
 			criteria.add(
 					Restrictions.like("bumen", bumen,MatchMode.ANYWHERE)
 					);
 		}
 		String tablename=request.getParameter("tablename");  //发文时间
 		if (!"".equals(tablename) &&  tablename!=null){		 		
 			criteria.add(
 					Restrictions.like("tablename", tablename,MatchMode.ANYWHERE)
 					);
 		}
		String createdatestr=request.getParameter("createdatestr");  //发文时间
 		if (!"".equals(createdatestr) &&  createdatestr!=null){
 			String[] createdatestrArray = createdatestr.split("/");
 			createdatestr = createdatestrArray[2]+"-"+createdatestrArray[0]+"-"+createdatestrArray[1];
 			criteria.add(
 					Restrictions.sqlRestriction("convert(char(10),createdate,120) = ?", createdatestr, Hibernate.STRING)
 					);
 		}
 		String uploaddatestr=request.getParameter("uploaddatestr");  //上传时间
 		if (!"".equals(uploaddatestr) &&  uploaddatestr!=null){
 			String[] uploaddatestrArray = uploaddatestr.split("/");
 			uploaddatestr = uploaddatestrArray[2]+"-"+uploaddatestrArray[0]+"-"+uploaddatestrArray[1];
 			criteria.add(
 					Restrictions.sqlRestriction("convert(char(10),uploaddate,120) = ?", uploaddatestr, Hibernate.STRING)
 					);
 		}
		criteria.add(Restrictions.eq("state", 1));
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("bumenid"));
		orderlist.add(Order.desc("state"));
		orderlist.add(Order.desc("createdate"));
		PaginationSupport ps=qbcDao.findPageByCriteria(criteria,orderlist,Integer.parseInt(slimit), Integer.parseInt(sstart));
		return ps;
	}
	
	
	/**
	 * 添加规章制度内容
	 * @param rules
	 * @return 
	 */
	public Integer addRules(TRules rules){
		return  hibernateDao.save(rules);
	}
	
	/**
	 * 查询规章制度内容
	 * @param id
	 * @return 
	 */
	public TRules getRulesId(Integer id){
		return (TRules) hibernateDao.findById(TRules.class, id);
	}
	
	/**
	 * 添加规章制度内容
	 * @param rules
	 * @return 
	 */
	public void updateRules(TRules rules){
		hibernateDao.update(rules);

	}
	
}
